package com.tool.mylibrary.ui.nicedialog.dialogview;

import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import com.tool.mylibrary.R;
import com.tool.mylibrary.ui.nicedialog.BaseNiceDialog;
import com.tool.mylibrary.ui.nicedialog.ViewHolder;

/**
 * 大部分的提交确认 弹窗
 * Created by Google on 2018/6/7.
 */

public class ConfirmDialog extends BaseNiceDialog {

    private String title, message, confirm, cancel;


    public OnOkListener onOkListener;

    @Override
    public int intLayoutId() {
        return R.layout.confirm_layout;
    }

    public static ConfirmDialog newInstance(String title, String message) {
        Bundle bundle = new Bundle();
        bundle.putString("title", title);
        bundle.putString("message", message);
        ConfirmDialog dialog = new ConfirmDialog();
        dialog.setArguments(bundle);
        return dialog;
    }

    public static ConfirmDialog newInstance(String title, String message, String confirm, String cancel) {
        Bundle bundle = new Bundle();
        bundle.putString("title", title);
        bundle.putString("message", message);
        bundle.putString("confirm", confirm);
        bundle.putString("cancel", cancel);
        ConfirmDialog dialog = new ConfirmDialog();
        dialog.setArguments(bundle);
        return dialog;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle == null) {
            return;
        }
        title = bundle.getString("title");
        message = bundle.getString("message");
        confirm = bundle.getString("confirm");
        cancel = bundle.getString("cancel");
        if (savedInstanceState != null) {
            onOkListener = savedInstanceState.getParcelable("listener");
        }
    }

    @Override
    public void convertView(ViewHolder holder, final BaseNiceDialog dialog) {
        if (!TextUtils.isEmpty(title)) holder.setText(R.id.title, title);
        if (!TextUtils.isEmpty(message)) holder.setText(R.id.message, message);
        if (!TextUtils.isEmpty(confirm)) holder.setText(R.id.ok, confirm);
        if (!TextUtils.isEmpty(cancel)) holder.setText(R.id.cancel, cancel);

        holder.setOnClickListener(R.id.cancel, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

        holder.setOnClickListener(R.id.ok, new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (onOkListener!=null){
                    onOkListener.onOk(dialog);
                }
            }
        });
    }

    public ConfirmDialog  setOnOkListener(OnOkListener onOkListener){
        this.onOkListener=onOkListener;
        return this;
    }

    /**
     * 保存接口
     *
     * @param outState
     */
    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putParcelable("listener", onOkListener);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        onOkListener = null;
    }


    public abstract static class OnOkListener implements Parcelable {

        protected abstract void onOk( BaseNiceDialog dialog);

        @Override
        public int describeContents() {
            return 0;
        }

        @Override
        public void writeToParcel(Parcel dest, int flags) {
        }

        public OnOkListener() {
        }

        protected OnOkListener(Parcel in) {
        }

        public final Creator<OnOkListener> CREATOR = new Creator<OnOkListener>() {
            @Override
            public OnOkListener createFromParcel(Parcel source) {
                return new OnOkListener() {
                    @Override
                    protected void onOk(BaseNiceDialog dialog) {

                    }
                };
            }

            @Override
            public OnOkListener[] newArray(int size) {
                return new ConfirmDialog.OnOkListener[size];
            }
        };
    }

}
